{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 监督学习\n",
    "对于训练数据及对应标签 $(X,y)$，学习映射函数 $f:X\\rightarrow y$\n",
    "- $y$ 连续时，回归问题\n",
    "- $y$ 离散时，分类问题\n",
    "\n",
    "数据的维度每增加 1，需要的数据量增加一个量级\n",
    "- $y=ax+b$，两个样本点$(x,y)$即可确定该直线\n",
    "- $y=a_1x_1+a_2x_2+b$，需要更多数据点$(x,y)$才可确定该平面\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 线性回归、逻辑回归\n",
    "\n",
    "线性回归为什么选择$MSE$作为目标函数（可选择的损失函数很多）\n",
    "- 凸函数，便于梯度下降优化，找到数值最优解"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数据预处理\n",
    "\n",
    "- 对于数据分布较均匀的特征可以选择归一化：$\\hat{x}=\\frac{x-\\overline{x}}{\\sigma}$\n",
    "- 对于偏态分布的数据，可以：$\\hat{x}=log(x)$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### KNN: K-Nearest Neighbors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "def distance(x1, x2):\n",
    "    return np.sum(np.sqrt((x1 - x2)**2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def model(X, y):\n",
    "    return [(xi, yi) for xi, yi in zip(X, y)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def predict(x, k=5):\n",
    "    return sorted(model(X, y), key=lambda xi: distance(xi[0], x))[:k]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 简单\n",
    "- k=1分类或回归\n",
    "- k>>1 带权重的分类或回归\n",
    "- 所有机器学习问题都可由此方法解决\n",
    "------------------\n",
    "- 维度灾难：高维空间时，不同点的欧几里得距离差别不大；计算量大，时间复杂度高\n",
    "- outliner 对结果影响很大\n",
    "- 模型需要保存所有样本，费内存"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 过拟合与欠拟合"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "欠拟合：模型过于简单，如一元二次方程数据集(x,y)，采用一次线性模型\n",
    "过拟合：\n",
    "- 数据量少，\n",
    "- 数据采样偏差\n",
    "- 模型过于复杂"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- Lazy-Learning：KNN 类模型，记住数据\n",
    "- Eager-Learning：神经网络，抽象出规律"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 性能评估"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- acc\n",
    "- precision\n",
    "- recall\n",
    "\n",
    "\n",
    "**todo**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Outliner"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 例如 KNN 模型，异常点对模型的效果影响很大\n",
    "- 简单的检测异常点方法：将数据排序，Percentile，比 25% 的数值小 1.5 倍，或比 75% 的数值大 1.5 倍的数据即为异常值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Bias 和 Variance"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- Variance：数据变化，结果的变化\n",
    "- Bias：模型与数据之间的偏差\n",
    "    - 模型越复杂，Bias 越小，Variance 越大\n",
    "    - 合适的模型复杂度， Bias+Variance 最小"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Train、validation、test"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Good Memory V.S Good Learning"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
